package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Goods;
import com.example.service.GoodsService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("goodss")
public class GoodsController {
    
    @Autowired
    private GoodsService goodsService;


    /**
     * 分页查询所有商品信息
     * @return
     */
    @GetMapping("/getPageGoods/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Goods goods){

        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<Goods> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(goods.getName()),Goods::getName,goods.getName());

        IPage page1 = goodsService.page(page,lqw);


        return R.success("分页查询所有goodss成功！",page1);
    }

    /**
     * 根据商品名查询商品信息
     * @return
     */
    @GetMapping("/getGoodsNumberByName")
    public R getGoodsNumberByName(Goods goods){

        LambdaQueryWrapper<Goods> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(goods.getName()),Goods::getName,goods.getName());
        Goods one = goodsService.getOne(lqw);

        return R.success("根据商品名查询商品信息！",one);
    }

    /**
     * 查询所有商品信息
     * @return
     */
    @GetMapping("/getAll")
    public R getAll(){

        List<Goods> list = goodsService.list();

        return R.success("查询所有商品信息！",list);
    }



    /**
     * 添加
     * @param goods
     * @return
     */
    @PostMapping("/addGoods")
    public R add(@RequestBody Goods goods){

        if(goods.getName()==null){
            return R.error("商品名不能为控！添加失败！");
        }

        LambdaQueryWrapper<Goods> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(goods.getName()),Goods::getName,goods.getName());
        Goods goods1=goodsService.getOne(lqw);

        if (goods1==null) {
            Boolean update = goodsService.save(goods);
            if (update) {
                return R.success("添加goods成功!", null);
            }
        }
        return R.error("商品已存在");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteGoods/{id}")
    public R deleteById(@PathVariable int id){

        //删除前 将这个删除的数据查到在删除，删除指挥将这个数据传给前端（记录日志要用到这个数据）
        LambdaQueryWrapper<Goods> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Goods::getId,id);
        Goods one = goodsService.getOne(lqw);

        Boolean byId = goodsService.removeById(id);
        if (byId){
            return R.success("删除goods成功!",one);
        }
        return R.error("删除goods失败!");
    }

    /**
     * 修改
     * @param goods
     * @return
     */
    @PutMapping("/updateGoods")
    public R deleteById(@RequestBody Goods goods){

        Boolean update = goodsService.updateById(goods);
        if (update){
            return R.success("修改goods成功!",goods);
        }
        return R.error("修改goods失败!");
    }


}

